<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="60px" @submit.native.prevent>
      <el-form-item label="标题" prop="title">
        <el-input v-model="pram.title" maxlength="100" placeholder="标题" />
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <Tinymce v-model="pram.content" :height="300"></Tinymce>
      </el-form-item>
      <el-form-item label="排序">
        <el-input-number v-model="pram.sort" :min="0"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handlerSubmit('pram')" v-hasPermi="['admin:notice:update','admin:notice:save']">{{ isCreate===0?'确定':'更新' }}</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce/index'
import  * as noticeApi from '@/api/noticeApi.js'
import * as roleApi from '@/api/role.js'
import {Debounce} from '@/utils/validate'
export default {
  // name: "edit"
  components: { Tinymce },
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return { rules: [] }
      }
    },
  },
  data() {
    return {
      constants:this.$constants,
      pram: {
        title: null,
        content: null,
        sort: 0,
        id: 0,
      },
      rules: {
        title: [{ required: true, message: '请填标题', trigger: ['blur', 'change'] }],
        content: [{ required: true, message: '请填内容', trigger: ['blur'] }],
      },
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideEditDialog')
    },
    initEditData() {
      if (this.isCreate !== 1) return //编辑
      const { title, sort, content, id } = this.editData //通过list中slot-scope=“scope“，scope.$index就是该行的下标，scope.row就是该行的数据所有消息对象，有了这两个参数就可以实现编辑、删除功能（this.editData就是scope.row传入的对象参数）
      this.pram.title = title
      this.pram.sort = sort
      this.pram.content = content
      this.pram.id = id
    },

    handlerSubmit:Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid) return
        if (this.isCreate === 0) {
          this.handlerSave()
        } else {
          this.handlerEdit()
        }
      })
    }),
    handlerSave() {
      noticeApi.incAdd(this.pram).then(data => {
        this.$message.success('创建通知公告成功')
        this.$emit('hideEditDialog')
      })
    },
    handlerEdit() {
      noticeApi.incAdd(this.pram).then(data => {
        this.$message.success('更新通知公告成功')
        this.$emit('hideEditDialog')
      })
    },
  }
}
</script>

<style scoped>

</style>
